<?php

namespace App\Services\Tools;

class NetEaseService {
    private $config;
    private $common_params;

    public function __construct() {
        $this->config = app("model_config")->getValue('NETEASE_LOGIN_CONFIG');
        $this->common_params = [
            'secretId'   => $this->config['SecretId'],
            'businessId' => $this->config['businessId'],
            'version'    => 'v1',
            'timestamp'  => getMillisecond(),
            'nonce'      => md5(NOW_TIME),
        ];
    }

    //一键登录接口
    public function check($token, $accessToken) {
        $url = 'https://ye.dun.163yun.com/v1/oneclick/check';
        $data = $this->common_params;
        $data['businessId'] = 'ff85e040bb8046e2912117d5fde67058';
        $data['token'] = $token;
        $data['accessToken'] = $accessToken;

        $signature = $this->gen_signature($this->config['SecretKey'], $data);
        $data['signature'] = $signature;
        $return = send_post_curl($url, $data);
        $this->log("----" . time_format() . "---<br/>url:" . $url . ";<br/>data:" . json_encode($data) . ";<br/>return:" . $return . "<br/>--------<br/>");
        return json_decode($return, true);
    }

    /**
     * 生成签名信息
     * $secretKey 产品私钥
     * $params 接口请求参数，不包括signature参数
     * @param $secretKey
     * @param $params
     * @return string
     */
    private function gen_signature($secretKey, $params) {
        ksort($params);
        $buff = "";
        foreach ($params as $key => $value) {
            $buff .= $key;
            $buff .= $value;
        }
        $buff .= $secretKey;
        return md5(mb_convert_encoding($buff, "utf8", "auto"));
    }

    /**
     * 图片过滤
     * @param $image
     * @param int $type 1普通图片2用户资料图片
     * @return array
     */
    public function checkImg($image, $type = 1) {
        $url = 'http://as.dun.163.com/v4/image/check';
        $data = $this->common_params;
        if ($type == 2) {
            $data['businessId'] = '1e9466121a2be680ee494a4c525ff66e';
        } else {
            $data['businessId'] = '6230401317575cd1da0694337a27f492';
        }

        $data['images'] = json_encode([$image]);
        $data['version'] = 'v4';
        $signature = $this->gen_signature($this->config['SecretKey'], $data);
        $data['signature'] = $signature;
        $return = send_post_curl($url, $data);
        $re = json_decode($return, true);
        if (!isset($re['code']) || $re['code'] != 200) {
            return ['code' => 0, 'msg' => $re['msg'] ?? '请求失败'];
        }
        //$re['antispam'][0]['action']:建议动作，2：建议删除，1：建议审核，0：建议通过
        return ['code' => 1, 'msg' => 'success', 'data' => $re['antispam'][0]['action'] ?? 0];
    }

    /**
     * 文本过滤接口
     * @param $content
     * @param int $type 1普通文本2用户资料文本
     * @return array action: -1请求失败,0通过,1嫌疑,2不通过
     */
    public function checkTxt($content, $type = 1) {
        $url = 'http://as.dun.163.com/v3/text/check';
        $data = $this->common_params;
        if ($type == 2) {
            $data['businessId'] = '7f37f8a658f4f96895692bacb3d28206';
        } else {
            $data['businessId'] = 'f8ac2974b9fbb3290d64aa43e2a91d6a';
        }
        $data['businessId'] = 'f8ac2974b9fbb3290d64aa43e2a91d6a';
        $data['version'] = 'v3.1';
        $data['dataId'] = md5(uniqid());
        $data['content'] = $content;

        $signature = $this->gen_signature($this->config['SecretKey'], $data);
        $data['signature'] = $signature;
        $return = send_post_curl($url, $data);
        $ret = json_decode($return, true);

        $action = -1;//-1请求失败,0通过,1嫌疑,2不通过
        if ($ret["code"] == 200) {
            $action = $ret["result"]["action"];
        }
        return ['action' => $action, 'callback_result' => $return];
    }

    public function log($content) {
//        $destination = C('LOG_PATH') . '../Netease/' . date('y_m_d') . '.log';
//
//        $log_dir = dirname($destination);
//        if (!is_dir($log_dir)) {
//            mkdir($log_dir, 0755, true);
//        }
//
//        error_log(str_replace('<br/>', "\r\n", $content), 3, $destination);
    }

    /**
     * 语音过滤
     * @param $audio_url
     * @return array
     */
    public function audioSubmit($audio_url) {
        $url = 'http://as.dun.163.com/v3/audio/submit';
        $data = $this->common_params;
        $data['businessId'] = '9b5a56f5a8f12b329a40abf34b9f0df7';
        $data['url'] = $audio_url;
        $data['version'] = 'v3.1';

        $signature = $this->gen_signature($this->config['SecretKey'], $data);
        $data['signature'] = $signature;
        send_post_curl($url, $data);
    }

    /**
     * 语音过滤
     * @param $audio_url
     * @return array
     */
    public function audioResult($audio_url) {
        $url = 'https://as.dun.163.com/v3/audio/callback/results';
        $data = $this->common_params;
        $data['businessId'] = '9b5a56f5a8f12b329a40abf34b9f0df7';
        $data['url'] = $audio_url;
        $data['version'] = 'v3';

        $signature = $this->gen_signature($this->config['SecretKey'], $data);
        $data['signature'] = $signature;
        send_post_curl($url, $data);
    }
}

